package com.amumu.drama.common.modules.fulu.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.amumu.drama.common.modules.fulu.dto.ProductStockQueryDto;
import com.amumu.drama.common.modules.fulu.dto.WXRefundQueryDto;
import com.amumu.drama.common.modules.fulu.dto.ZFBRefundQueryDto;
import com.amumu.drama.common.modules.fulu.dto.*;
import com.amumu.drama.common.modules.fulu.dto.base.BaseResp;
import com.amumu.drama.common.modules.fulu.dto.req.DirectOrderInfoQueryReq;
import com.amumu.drama.common.modules.fulu.dto.resp.DirectOrderInfoQueryResp;
import com.amumu.drama.common.modules.fulu.dto.resp.DirectOrderResp;
import com.amumu.drama.common.modules.fulu.dto.resp.FuluProductResp;
import fulu.sup.open.api.model.InputDirectOrderDto;
import fulu.sup.open.api.model.InputOrderGetDto;
import fulu.sup.open.api.model.InputProductTemplateDto;
import fulu.sup.open.api.sdk.DefaultOpenApiClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author sunzhuang
 * @date 2024/5/28 15:23
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc todo
 */
@Slf4j
@Component
public class FuluService {
    @Value("${remote-service.fulu.url:http://openapi.fulu.com/api/getway}")
    private String url;

    @Value("${remote-service.fulu.appKey:hZoWVxglk3tg4Bb2OZ+ElIB1B2xF6/Jre3Shhs2/xgX/rFfMYW8coXgMr5HCQ9QN}")
    private String appKey;

    @Value("${remote-service.fulu.sysSecret:55e7283678e54939b9830362fc5423fb}")
    private String sysSecret;


    /**
     * 获取sku商品填写模板信息
     *
     * @return
     */
    public List<FuluProductResp> queryProductList() {
        ProductStockQueryDto dto = new ProductStockQueryDto();
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, "fulu.goods.list.get");
        client.setBizObject(dto);
        String result = client.excute();
        log.info("获取sku商品填写模板信息结果如下:{}", result);
        JSONObject jsonResult = JSONUtil.parseObj(result);
        try {
            return jsonResult.getJSONArray("result").toList(FuluProductResp.class);
        } catch (Exception e) {
            String message = jsonResult.getStr("message");
            throw new RuntimeException(StrUtil.isBlank(message) ? result : message);
        }
    }

    /**
     * 获取sku商品填写模板信息
     *
     * @return
     */
    public String getProductTemplate(String templateId) {
        InputProductTemplateDto dto = new InputProductTemplateDto();
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, dto.getMethod());
        dto.setTemplateId(templateId);
        client.setBizObject(dto);
        String result = client.excute();
        log.info("获取sku商品填写模板信息结果如下:{}", result);
        JSONObject jsonResult = JSONUtil.parseObj(result);
        return JSONUtil.toJsonStr(jsonResult.getJSONObject("result"));
    }


    /**
     * 获取sku商品填写模板信息
     *
     * @return
     */
    public JSONObject getProductTemplateToJSONObject(String templateId) {
        InputProductTemplateDto dto = new InputProductTemplateDto();
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, dto.getMethod());
        dto.setTemplateId(templateId);
        client.setBizObject(dto);
        String result = client.excute();
        log.info("获取sku商品填写模板信息结果如下:{}", result);
        JSONObject jsonResult = JSONUtil.parseObj(result, true);
        return jsonResult.getJSONObject("result");
    }

    /**
     * 直充下单接口
     *
     * @param diRectDto
     * @return
     */
    public BaseResp<DirectOrderResp> fuLuOrderDirect(InputDirectOrderBusinessDto diRectDto) {
        InputDirectOrderDto dto = new InputDirectOrderDto();
        dto.setProductId(Integer.parseInt(diRectDto.getProductId()));
        dto.setCustomerOrderNo(diRectDto.getCustomerOrderNo());
        dto.setChargeAccount(diRectDto.getChargeAccount());
        dto.setBuyNum(diRectDto.getBuyNum());
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, dto.getMethod());
        client.setBizObject(dto);
        String result = client.excute();
        log.info("直充下单接口结果如下:{}", result);
        if (StrUtil.isBlank(result)) {
            log.error("直充下单接口结果为空");
            return null;
        }
        JSONObject jsonResult = JSONUtil.parseObj(result);
        BaseResp<DirectOrderResp> resp = jsonResult.toBean(BaseResp.class);
        String resultStr = jsonResult.getStr("result");
        if (StrUtil.isNotBlank(resultStr)) {
            resp.setResult(JSONUtil.toBean(resultStr, DirectOrderResp.class));
        } else {
            resp.setResult(null);
        }
        return resp;
    }


    /**
     * 直充下单接口
     *
     * @param req 外部订单号
     * @return
     */
    public BaseResp<DirectOrderInfoQueryResp> queryOrderInfo(DirectOrderInfoQueryReq req) {
        InputOrderGetDto dto = new InputOrderGetDto();
        dto.setCustomerOrderNo(req.getCustomerOrderNo());
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, dto.getMethod());
        client.setBizObject(dto);
        String result = client.excute();
        log.info("查询福禄订单信息:{}", result);
        if (StrUtil.isBlank(result)) {
            log.error("查询福禄订单信息为空");
            return null;
        }
        JSONObject jsonResult = JSONUtil.parseObj(result);
        BaseResp<DirectOrderInfoQueryResp> resp = jsonResult.toBean(BaseResp.class);
        resp.setResult(jsonResult.getJSONObject("result").toBean(DirectOrderInfoQueryResp.class));
        return resp;
    }

    /**
     * 商品库存校验接口
     *
     * @param productStockCheckDto
     * @return
     */
    public String fuLuProductStockCheck(ProductStockCheckDto productStockCheckDto) {
        ProductStockQueryDto dto = new ProductStockQueryDto();
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, dto.getMethod());
        dto.setProductId(productStockCheckDto.getProductId());
        dto.setBuyNum(productStockCheckDto.getBuyNum());
        client.setBizObject(dto);
        String result = client.excute();
        log.info("商品库存校验接口结果如下:{}", result);
        return result;
    }

    /**
     * WX退款接口
     *
     * @param wxRefundDto
     * @return
     */
    public String fuLuProductStockCheckForWX(WXRefundDto wxRefundDto) {
        WXRefundQueryDto dto = new WXRefundQueryDto();
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, dto.getMethod());
        dto.setOutTradeNo(wxRefundDto.getOutTradeNo());
        dto.setOutRefundNo(wxRefundDto.getOutRefundNo());
        dto.setRefundFee(wxRefundDto.getRefundFee());
        dto.setTotalFee(wxRefundDto.getTotalFee());
        client.setBizObject(dto);
        String result = client.excute();
        log.info("WX退款接口结果如下:{}", result);
        return result;
    }


    /**
     * ZFB退款接口
     *
     * @param zfbRefundQueryDto
     * @return
     */
    public String fuLuProductStockCheckForZFB(ZFBRefundDto zfbRefundQueryDto) {
        ZFBRefundQueryDto dto = new ZFBRefundQueryDto();
        DefaultOpenApiClient client =
                new DefaultOpenApiClient(url, appKey, sysSecret, dto.getMethod());
        dto.setOutTradeNo(zfbRefundQueryDto.getOutTradeNo());
        dto.setRefundAmount(zfbRefundQueryDto.getRefundAmount());
        client.setBizObject(dto);
        String result = client.excute();
        log.info("ZFB退款接口结果如下:{}", result);
        return result;
    }
}
